﻿using System;
using System.Collections.Generic;
using System.Text;
using NLBS.Entity;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Data;
using System.Data.Common;
using NHibernate.Criterion;

namespace NLBS.Data
{
    public partial class NHibernateDataProvider : DbProvider
    {
        #region guestBook

        public override IList<GuestBook> GetGuestBook()
        {
           return CurrentSession.CreateCriteria(typeof(GuestBook)).List<GuestBook>();
        }

        public override IPagedCollection<GuestBook> GetGuestBookBySearch(int startIndex, int pageSize, string prefix)
        {
            StringBuilder sb = new StringBuilder();
            string[] prefixArray = prefix.Split(new char[] { ' ' });

            for (int i = 0; i < prefixArray.Length; i++)
            {
                if (i > 0)
                {
                    sb.Append("AND ");
                }
                sb.AppendFormat("gb_content LIKE '%{0}%'", prefixArray[i]);
            }
            //comm_content
            DbCommand dbCommand = db.GetSqlStringCommand(string.Format(@"SELECT top {0} gb.*, 
            FROM [Guestbook] gb 
            WHERE gb_id<=(select min(gb_id) 
            from (select top {1} gb_id from Guestbook order by gb_id desc) as T) AND {2} 
            ORDER BY gb_postTime DESC;
            SELECT COUNT(gb_id) AS TotalRecords from Guestbook where {2}", startIndex, pageSize, sb.ToString()));
            IDataReader dr = db.ExecuteReader(dbCommand);
            IPagedCollection<GuestBook> lists = new PagedCollection<GuestBook>();
            GuestBook gb = null;
            while (dr.Read())
            {
                gb = CommonDataProvider.PopulateGuestBookFromIDataReader(dr);
                //comment.Article = PopulateCommentArticleFromIDataReader(dr);
                lists.Add(gb);
            }
            lists.MaxItems = CommonDataProvider.PopulateTotalRecordsFromIDataReader(dr);
            return lists;
        }

        public override IList<GuestBook> GetTopGuestBook()
        {
            return CurrentSession.CreateCriteria(typeof(GuestBook)).
                SetMaxResults(10)
                .List<GuestBook>();
        }

        public override GuestBook GetGuestBookByID(int gbookID)
        {
            return CurrentSession.Get<GuestBook>(gbookID);
        }

        public override int InsertGuestBook(GuestBook gbook)
        {
            CurrentSession.Save(gbook);
            CurrentSession.Flush();
            return 1;
        }

        public override void UpdateGuestBook(GuestBook gbook)
        {
            CurrentSession.Update(gbook);
            CurrentSession.Flush();
        }

        public override void DeleteGuestBook(int gbookID)
        {
            CurrentSession.Delete(GetGuestBookByID(gbookID));
            CurrentSession.Flush();
        }

        public override int GetAllGuestBookCount()
        {
            return Convert.ToInt32(CurrentSession.
                CreateQuery("select count(*) from GuestBook as g").
                UniqueResult());
        }

        public override IPagedCollection<GuestBook> GetAllGuestBookByPage(int startIndex, int pageSize)
        {
            IList<GuestBook> list = CurrentSession.CreateCriteria(typeof(GuestBook))
                  .SetFirstResult(startIndex)
                  .SetMaxResults(pageSize).List<GuestBook>();

            IPagedCollection<GuestBook> lists = new PagedCollection<GuestBook>(list);
            lists.MaxItems = GetAllGuestBookCount();
            return lists;
        }

        #endregion
    }
}
